Skip to content

Conversation

@lifefire1
Copy link

Description

Hi Spring team!

I'm a frequent Spring user and I really enjoy working with it. However, I often find myself writing boilerplate code to validate file extensions when handling file uploads.

To solve this, I've created an @AcceptableExtension annotation that allows easy declarative validation of file extensions.

Example Usage

Before:

@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) {
    String filename = file.getOriginalFilename();
    String extension = filename.substring(filename.lastIndexOf(".") + 1);
    if (!List.of("jpg", "png", "pdf").contains(extension.toLowerCase())) {
        throw new IllegalArgumentException("Invalid file extension");
    }
    // process file...
    return "success";
}

After:

@PostMapping("/upload")
public String upload(
        @AcceptableExtension(extensions = {"jpg", "png", "pdf"})
        @RequestParam("file") MultipartFile file) {
    // file is already validated
    return "success";
}

What's Included

  • @AcceptableExtension annotation with customizable extensions and error messages
  • AcceptableExtensionMethodArgumentResolver for validation logic
  • Comprehensive test suite (15+ test cases)
  • Case-insensitive extension matching

All tests pass successfully.

Thank you for considering this contribution!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 8, 2025
return true;
}
}
return false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    if (acceptableExtensions.length == 0) {
        return true;
    }

    return Arrays.stream(acceptableExtensions)
            .anyMatch(acceptable -> acceptable.equalsIgnoreCase(extension));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment has been corrected

Signed-off-by: Алексей Яхненко <firend6334@gmail.com>
@lifefire1 lifefire1 force-pushed the feature/addAceptableExtension branch from 3ec4588 to 3f1a2ed Compare November 12, 2025 05:05
@bclozel
Copy link
Member

bclozel commented Nov 17, 2025

Thanks for this proposal, but I don't think this use case is common enough that it should be included in Spring Framework by default. Maybe try and share this as a library first for the community? We can reconsider if there is enough demand for this enhancement.

Thanks!

@bclozel bclozel closed this Nov 17, 2025
@bclozel bclozel added in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants